package com.heima.wemedia.controller.v1;

import com.heima.api.wemedia.WmNewsControllerApi;
import com.heima.common.constants.wemedia.WemediaConstants;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.wemedia.dtos.NewsAuthDto;
import com.heima.model.wemedia.dtos.WmNewsDto;
import com.heima.model.wemedia.dtos.WmNewsPageReqDto;
import com.heima.model.wemedia.pojos.WmNews;
import com.heima.wemedia.service.WmNewsService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * @ClassName WnNewsController
 * @Description TODO
 * @Author AWei
 * @DATE 2021/4/15 17:30
 * @Version 1.0
 */
@RestController
@RequestMapping("api/v1/news")
public class WmNewsController implements WmNewsControllerApi {

    @Resource
    private WmNewsService wmNewsService;

    /**
     * @param wmNewsPageReqDto 请求携带数据封装对象
     * @return 查询结果
     * @Description 分页带条件查询自媒体文章列表
     */
    @Override
    @PostMapping("/list")
    public ResponseResult findAllNewsByAnyCondition(@RequestBody WmNewsPageReqDto wmNewsPageReqDto) {
        return wmNewsService.findAllNewsByAnyCondition(wmNewsPageReqDto);
    }

    /**
     * @param wmNewsDto 文章编写请求携带参数封装对象
     * @return 提交结果
     * @Description 提交文章
     */
    @Override
    @PostMapping("/submit")
    public ResponseResult submitNews(@RequestBody WmNewsDto wmNewsDto) {
        if (WemediaConstants.WM_NEWS_SUMMIT_STATUS.equals(wmNewsDto.getStatus())) {
            return wmNewsService.submitNews(wmNewsDto, WemediaConstants.WM_NEWS_SUMMIT_STATUS);
        } else {
            return wmNewsService.submitNews(wmNewsDto, WemediaConstants.WM_NEWS_DRAFT_STATUS);
        }
    }

    @Override
    @GetMapping("/one/{id}")
    public ResponseResult findWmNewsById(@PathVariable("id") Integer id) {
        return wmNewsService.findWmNewsById(id);
    }

    @Override
    @GetMapping("/del_news/{id}")
    public ResponseResult delNews(@PathVariable("id") Integer id) {
        return wmNewsService.delNews(id);
    }

    @Override
    @PostMapping("/down_or_up")
    public ResponseResult downOrUp(@RequestBody WmNewsDto dto) {
        return wmNewsService.downOrUp(dto);
    }

    /**
     * @param id 文章id
     * @return 查询结果
     * @Description 根据id查询文章
     */
    @Override
    @GetMapping("/findOne/{id}")
    public WmNews findById(@PathVariable("id") Integer id) {
        return wmNewsService.getById(id);
    }

    /**
     * @param wmNews 文章对象
     * @return 更新结果
     * @Description 修改文章
     */
    @Override
    @PutMapping("/update")
    public ResponseResult updateWmNews(@RequestBody WmNews wmNews) {
        wmNewsService.updateById(wmNews);
        return ResponseResult.okResult();
    }

    /**
     * @return 查询结果
     * @Description 查询需要发布的文章id列表
     */
    @Override
    @GetMapping("/findRelease")
    public List<Integer> findRelease() {
        return wmNewsService.findRelease();
    }

    /**
     * @param newsAuthDto 文章列表分页查询对象
     * @return 分页查询结果集
     * @Description 查询文章列表
     */
    @Override
    @PostMapping("/list_vo")
    public ResponseResult findList(@RequestBody NewsAuthDto newsAuthDto) {
        return wmNewsService.findList(newsAuthDto);
    }

    /**
     * @param id 文章id
     * @return 查询结果
     * @Description 查询文章详情
     */
    @GetMapping("/one_vo/{id}")
    @Override
    public ResponseResult findWmNewsVo(@PathVariable("id") Integer id) {
        return wmNewsService.findWmNewsVo(id);
    }

    /**
     * @param newsAuthDto 文章列表分页查询条件对象
     * @return 响应结果
     * @Description 文章审核失败
     */
    @Override
    @PostMapping("/auth_pass")
    public ResponseResult authPass(@RequestBody NewsAuthDto newsAuthDto) {
        return wmNewsService.updateStatus(WemediaConstants.WM_NEWS_AUTH_PASS,newsAuthDto);
    }

    /**
     * @param newsAuthDto 文章列表分页查询条件对象
     * @return 响应结果
     * @Description 文章审核失败
     */
    @Override
    @PostMapping("/auth_fail")
    public ResponseResult authFail(@RequestBody NewsAuthDto newsAuthDto) {
        return wmNewsService.updateStatus(WemediaConstants.WM_NEWS_AUTH_FAIL,newsAuthDto);
    }

}